library AmmoEmpty initializer init requires LegacySystem, AmmoAPI

	
	private function NoAmmo takes nothing returns nothing
		local player p = GetTriggerPlayer()
		local integer i = GetConvertedPlayerId(p)
		if AmmoSystemMagazines[i]<1 and GetWidgetLife(udg_Hero[i])>0.405 and GetUnitTypeId(udg_Hero[i])!='H000' then
			set udg_Reloading[i] = 3
			set udg_soundpath = "war3mapImported\\NoAmmo.wav"
			call PlaySoundPath.execute()
			call DisplayTimedTextToPlayer( GetLocalPlayer() ,0.0 ,0.0, 2.0, "|cfff3ad00ALICE|r : " + GetPlayerName(p) + " has run out of ammunition." )
			call UnitAddAbility(udg_Hero[i],'A02H')
		endif
	endfunction
	
	private function EmptyMagazine takes nothing returns nothing
		local unit u = GetAttacker()
		if udg_Reloading[GetConvertedPlayerId(GetOwningPlayer(u))]!=0 and IsUnitType(u,UNIT_TYPE_HERO)==true then
			call IssueImmediateOrder(u,"holdposition")
		endif
		set u = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set AmmoEmptyTrigger = CreateTrigger()
		call TriggerAddAction(AmmoEmptyTrigger,function NoAmmo)
		
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_ATTACKED)
		call TriggerAddAction(t,function EmptyMagazine)
	endfunction
endlibrary

